<?php

/**
 * @author Dingo
 */
class Form_Aktualnosci {

    private $form;

    public function __construct() {
        $Config = array(
            // Default fields size
            'size' => 35,
            // Default fields error css
            'css_error' => 'fields_error',
            'template_open' => '<div class="form_row">',
            'template_label' => '<label>|LABEL|</label>',
            'template_field' => '|FIELD|',
            'template_error' => '<BR><font class="error">- |ERROR|</font>',
            'template_close' => '</div>',
            'template_line' => "\n",
            'template_field_one' => '<div class="form_row">|LABEL||FIELD|</div>'
        );
        $form = & new Form_AuriumForm($Config);
        $this->form = $form;
    }

    public function generujAktualnosci(Aktualnosci $aktualnosci) {
        $id = array(
            'type' => 'hidden',
            'name' => 'id',
            'value' => $aktualnosci->id);

        $tytul = array(
            'label' => 'Tytuł*:',
            'type' => 'text',
            'name' => 'tytul',
            'value' => htmlspecialchars(isset($_POST['tytul']) ? $_POST['tytul'] : $aktualnosci->tytul));

        $url = array(
            'label' => 'URL*:',
            'type' => 'text',
            'name' => 'url',
            'value' => htmlspecialchars(isset($_POST['url']) ? $_POST['url'] : $aktualnosci->url));

        $wygenerujUrl = array(
            'label' => 'Generuj URL:',
            'type' => 'button',
            'value' => 'Wygeneruj',
            'subtype' => 'button',
            'extras' => 'onClick="generujUrl();"',
            'name' => 'generuj'
        );

        $zajawka = array(
            'label' => 'Zajawka*:',
            'type' => 'textarea',
            'name' => 'zajawka',
            'size' => array(35, 6),
            'value' => htmlspecialchars(isset($_POST['zajawka']) ? $_POST['zajawka'] : $aktualnosci->zajawka));

        $tresc = array(
            'label' => 'Treść*:',
            'type' => 'textarea',
            'name' => 'tresc',
            'size' => array(35, 20),
            'value' => htmlspecialchars(isset($_POST['tresc']) ? $_POST['tresc'] : $aktualnosci->tresc));

        $obrazek = array(
            'label' => 'URL obrazka:',
            'type' => 'text',
            'name' => 'obrazek',
            'value' => htmlspecialchars(isset($_POST['obrazek']) ? $_POST['obrazek'] : $aktualnosci->obrazek));

        $pokazuj = array(
            'label' => 'Pokazuj:',
            'type' => 'checkbox',
            'name' => 'status',
            'value' => '1',
            'text_clickable' => true,
            'selected' => isset($_POST['status']) ? $_POST['status'] : $aktualnosci->status);

        $this->form->SetData(array($tytul, $wygenerujUrl, $url, $zajawka, $tresc,
            $obrazek, $pokazuj, $id));
        $formularz = $this->form->ShowFields();
        return $formularz;
    }

    public function generujTagi($tagi) {
        $formularz = '<div id="tagi">';

        $dodajWiecej = array(
            'type' => 'button',
            'label' => 'Nowy tag:',
            'value' => 'Dodaj nowy',
            'subtype' => 'button',
            'extras' => 'onClick="dodajTag()"'
        );

        /* Generowanie tagów - edycja aktualności */
        foreach ($tagi as $k => $v) {
            $formularz .= '<div class="form_row"><label>Tag '.($k+1).':</label>' .
                    '<input type="text" name="tag[]" size="35" ' .
                    'value="' . htmlentities($v) . '">' .
                    '<input type="button" value="Usuń" name="tag_del" ' .
                    'onClick="$(this).parent().slideUp(\'slow\', function() ' .
                    '{ $(this).remove() } );return false;"></div>';
        }

        $formularz .= '</div>';
        $formularz .= $this->form->OneField($dodajWiecej);
        return $formularz;
    }

    /**
     *
     * @param <type> $action link do akcji w znaczniku form
     * @return <type> Zwraca otwarcie forma
     */
    public function otworzForma($action) {
        $formularz = '<div class="form">';
        $formularz .= $this->form->OpenForm(array('m' => 'p',
                    'action' => $action));
        return $formularz;
    }

    public function zamknijForma() {
        $przyciski = array(
            'type' => 'button',
            'subtype' => array('submit', 'button'),
            'value' => array('Wyślij', 'Anuluj'),
            'extras' => array('', 'onClick="window.location = \'/cms/aktualnosci/\';"'),
            'alone' => true
        );
        $formularz = $this->form->OneField($przyciski);
        $formularz .= $this->form->CloseForm();
        $formularz .= '</div>';
        return $formularz;
    }

}

?>
